package com.clouds.wms.cms;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsmh;
import com.clouds.wms.bean.Cmsmi;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmiService;
import com.clouds.wms.service.CmsmkService;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.RequestHelper;
import com.clouds.wms.util.SysParams;

@Controller
@RequestMapping("/cmsmi")
public class CmsmiController {

	private Log log = LogFactory.getLog(getClass());
	
	@Resource(name="cmsmiService")
	private CmsmiService cmsmiService;

	@Resource(name="cmsmkService")
	private CmsmkService cmsmkService;
	
	/**
	 * 库位信息
	 * @author dz.wang
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/cmsmiMain.xhtml")
	public String cmsmiMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		model.addAttribute("mhaa", RequestHelper.getString(request, "mhaa"));
		return "cms/cmsmi";
	}
	
	/**
	 * 库位信息
	 * @author dz.wang
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/cmsmiList.xhtml")
	public String cmsmiList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mhaa = RequestHelper.getString(request, "mhaa");
		String miaa = RequestHelper.getString(request, "miaa");
		int miad = RequestHelper.getInt(request, "miad", 0);
		model.addAttribute("mhaa",mhaa);
		int pageNumber = RequestHelper.getInt(request, "page", 1);// 要跳转的页号
		int pageSize = RequestHelper.getInt(request, "rows", 10);// 一页的条数
		PagesBean pb = cmsmiService.getCmsmiListByMhaa(mhaa,miaa, miad, pageNumber, pageSize);
		List<Cmsmi> list = ((List<Cmsmi>)pb.getData());
		int count = pb.getCount();
		Map<String,Object> paraMap = new HashMap<String,Object>();
		paraMap.put("total", count);
		paraMap.put("rows", list);
		try {
			Function.toJson(response, paraMap);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		return null;
	}
	
	/**
	 * 添加仓库
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/saveCmsmi.xhtml", method = RequestMethod.POST)
	public String saveCmsmi(HttpServletRequest request, HttpServletResponse response, Model model, Cmsmi cmsmi) throws SysException, IOException {
		String miaa = cmsmi.getMiaa();
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if(miaa == null || "".equals(miaa.trim())) {
			out.print("库位编号不能为空!");
			return null;
		}
		Cmsmi cmsmitmp = cmsmiService.getCmsmiByMiaa(miaa);
		if(cmsmitmp != null && cmsmitmp.getMiaa() != null && !"".equals(cmsmitmp.getMiaa())) {
			out.print("仓库编号已经存在!");
			return null;
		}
		cmsmiService.saveCmsmi(cmsmi);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("增加了编号为[" + miaa + "]的库位!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	
	/**
	 * 编辑库位
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/updateCmsmi.xhtml", method = RequestMethod.POST)
	public String updateCmsmi(HttpServletRequest request, HttpServletResponse response, Model model, Cmsmi cmsmi) throws SysException, IOException {
		String miaa = cmsmi.getMiaa();
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if(miaa == null || "".equals(miaa.trim())) {
			out.print("库位编号不能为空!");
			return null;
		}
		cmsmiService.updateCmsmi(cmsmi);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("编辑了编号为[" + miaa + "]的库位!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}

	/**
	 * 禁用库位
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/disableCmsmi.xhtml", method = RequestMethod.POST)
	public String disableCmsmi(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String miaas = RequestHelper.getString(request, "miaa");
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		String[] miaaStr = miaas.split(",");
		for(String miaa : miaaStr) {
			cmsmiService.updateCmsmiMiag(miaa, SysParams.STATUS_DISABLE);
			Cmsmk cmsmk = new Cmsmk();
			cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
			cmsmk.setMkac(globalUmUser.getMdac());
			cmsmk.setMkad("禁用了编号为[" + miaas + "]的仓库!");
			cmsmk.setMkae(new Date());
			cmsmk.setMkaf(request.getRemoteAddr());
			cmsmkService.saveCmsmk(cmsmk);
		}
		out.print("success");
		return null;
	}

	/**
	 * 启用库位
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/startUsingCmsmi.xhtml", method = RequestMethod.POST)
	public String startUsingCmsmi(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String miaas = RequestHelper.getString(request, "miaa");
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		String[] miaaStr = miaas.split(",");
		for(String miaa : miaaStr) {
			cmsmiService.updateCmsmiMiag(miaa, SysParams.STATUS_NORMAL);
			Cmsmk cmsmk = new Cmsmk();
			cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
			cmsmk.setMkac(globalUmUser.getMdac());
			cmsmk.setMkad("启用了编号为[" + miaas + "]的仓库!");
			cmsmk.setMkae(new Date());
			cmsmk.setMkaf(request.getRemoteAddr());
			cmsmkService.saveCmsmk(cmsmk);
		}
		out.print("success");
		return null;
	}
	
	/**
	 * 库位信息
	 * @author dz.wang
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/cmsmiListByMhaa.xhtml")
	public String cmsmiListByMhaa(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mhaa = RequestHelper.getString(request, "mhaa");
		String ecaa = RequestHelper.getString(request, "ecaa");
		String ecad = RequestHelper.getString(request, "ecad");
		String ecae = RequestHelper.getString(request, "ecae");
		if("null".equals(ecad)) {
			ecad = "";
		}
		
		List<Cmsmi> list = cmsmiService.getAllCmsmiList(mhaa, SysParams.STATUS_NORMAL, ecaa, ecad, ecae);

		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			String datas = "[";
			for(Cmsmi cmsmi : list) {
				datas += "{\"value\":\""+cmsmi.getMiaa()+"\",\"text\":\""+cmsmi.getMiaa()+"\"},";
			}
			datas += "]";
			out.print(datas.replace("\"},]", "\"}]"));
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return null;
	}
	/**
	 * 根据仓库编号查找库位信息
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/cmsmiListByMiaa.xhtml")
	public String cmsmiListByMiaa(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String miaa = RequestHelper.getString(request, "miaa");
		List<Cmsmi> list = cmsmiService.getCmsmiListByMiaa(miaa);
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			String datas = "[";
			for(Cmsmi cmsmi : list) {
				datas += "{\"value\":\""+cmsmi.getMiaa()+"\",\"text\":\""+cmsmi.getMiaa()+"\"},";
			}
			datas += "]";
			out.print(datas.replace("\"},]", "\"}]"));
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return null;
	}
}
